package org.jeecg.modules.online.desform.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.online.desform.constant.WidgetTypes;
import org.jeecg.modules.online.desform.entity.DesignForm;
import org.jeecg.modules.online.desform.service.IDesignFormService;
import org.jeecg.modules.online.desform.service.IDesignFormToolsService;
import org.jeecg.modules.online.desform.util.DesformUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 表单设计器各种工具
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2019-05-16
 */
@Slf4j
@RestController("designFormToolsController")
@RequestMapping("/desform/tools")
public class DesignFormToolsController {

    @Autowired
    IDesignFormToolsService toolsService;
    @Autowired
    IDesignFormService designFormService;

    /**
     * 获取某个表单的所有组件的基本信息
     *
     * @param desformCode
     * @return 返回一个数组，数组的每项包含：type、 key、 name、 model
     */
    @GetMapping("/getWidgetInfoLite")
    public Result doGetWidgetInfoLite(
            @RequestParam("desformCode") String desformCode,
            // 筛选条件，过滤type，可逗号分割，可不传，如果传了就只返回等于该type的组件
            @RequestParam(name = "filterType", required = false) String filterTypeParam
    ) {
        DesignForm designForm = designFormService.getByCode(desformCode);
        if (designForm == null) {
            return Result.error("不存在的表单");
        }
        // 处理type过滤条件
        List<WidgetTypes> filterTypes = new ArrayList<>();
        if (StringUtils.isNotBlank(filterTypeParam)) {
            String[] types = filterTypeParam.split(",");
            for (String type : types) {
                WidgetTypes filterType = WidgetTypes.getByValue(type);
                if (filterType != null) {
                    filterTypes.add(filterType);
                }
            }
        }
        JSONArray array = new JSONArray();
        DesformUtils.recursiveAllWidget(designForm, (type, item, parent) -> {
            // 过滤条件
            if (filterTypes.size() > 0 && !filterTypes.contains(type)) {
                return;
            }
            JSONObject resItem = new JSONObject();
            resItem.put("key", item.get("key"));
            resItem.put("type", item.get("type"));
            resItem.put("name", item.get("name"));
            resItem.put("model", item.get("model"));
            array.add(resItem);
        });
        return Result.ok(array);
    }

    /**
     * 【索引工具】修改数据绑定Key
     */
    @PutMapping("/index/changeModel")
    public Result doIndexChangeModel(@RequestBody JSONObject data) {
        if (data == null) {
            return Result.error("data不能为空");
        }
        String oldKey = data.getString("oldKey");
        String newKey = data.getString("newKey");
        String desformCode = data.getString("desformCode");

        if (StringUtils.isBlank(oldKey) || StringUtils.isBlank(newKey)) {
            return Result.error("以下参数不能为空：oldKey|newKey|desformCode");
        }


        return toolsService.indexChangeModel(desformCode, oldKey, newKey);
    }


    /**
     * 【索引工具】修改数据格式
     */
    @PutMapping("/index/formatData")
    public Result doIndexChangeData(@RequestBody JSONObject data) {
        if (data == null) {
            return Result.error("data不能为空");
        }
        String widgetType = data.getString("widgetType");
        String action = data.getString("action");
        String model = data.getString("model");
        String desformCode = data.getString("desformCode");

        if (StringUtils.isBlank(widgetType) || StringUtils.isBlank(action) || StringUtils.isBlank(model)) {
            return Result.error("以下参数不能为空：widgetType|action|model|desformCode");
        }

        return toolsService.indexFormatData(desformCode, widgetType, action, model);
    }

}
